{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vgg = np.load('VGG_imagenet.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(vgg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "vgg = np.load('VGG_imagenet.npy').item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['conv5_1',\n",
       " 'fc6',\n",
       " 'conv5_3',\n",
       " 'fc7',\n",
       " 'fc8',\n",
       " 'conv5_2',\n",
       " 'conv4_1',\n",
       " 'conv4_2',\n",
       " 'conv4_3',\n",
       " 'conv3_3',\n",
       " 'conv3_2',\n",
       " 'conv3_1',\n",
       " 'conv1_1',\n",
       " 'conv1_2',\n",
       " 'conv2_2',\n",
       " 'conv2_1']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vgg.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "mv = vgg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3, 3, 64)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nmv['conv1_1']['weights'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "for key in vgg.keys():\n",
    "    if 'conv' in key:\n",
    "        new_name = key + '_' + str(2)\n",
    "        mv[new_name] = vgg[key]\n",
    "    if 'fc' in key and '8' not in key:\n",
    "        for i in range(1, 3):\n",
    "            ind = np.random.randint(4096, size=2048)\n",
    "            new_key = key + '_' + str(i)\n",
    "            mv[new_key] = {'weights':None, 'biases':None}\n",
    "            if '6' in  key:\n",
    "                mv[new_key]['weights'] = vgg[key]['weights'][:,ind]\n",
    "                mv[new_key]['biases'] = vgg[key]['biases'][ind]\n",
    "            else :\n",
    "                mv[new_key]['weights'] = vgg[key]['weights'][ind,:]\n",
    "                mv[new_key]['biases'] = vgg[key]['biases'][ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['fc6_1',\n",
       " 'fc6',\n",
       " 'fc6_2',\n",
       " 'fc8',\n",
       " 'fc7_1',\n",
       " 'fc7_2',\n",
       " 'conv4_1_2',\n",
       " 'conv4_2_2',\n",
       " 'conv1_1',\n",
       " 'conv1_2',\n",
       " 'conv2_1_2',\n",
       " 'conv3_1_2',\n",
       " 'conv3_2_2',\n",
       " 'conv5_1',\n",
       " 'conv5_3',\n",
       " 'conv5_2',\n",
       " 'conv3_3_2',\n",
       " 'fc7',\n",
       " 'conv4_1',\n",
       " 'conv4_2',\n",
       " 'conv4_3',\n",
       " 'conv2_2_2',\n",
       " 'conv3_3',\n",
       " 'conv3_2',\n",
       " 'conv3_1',\n",
       " 'conv2_2',\n",
       " 'conv2_1',\n",
       " 'conv5_2_2',\n",
       " 'conv5_3_2',\n",
       " 'conv4_3_2',\n",
       " 'conv5_1_2',\n",
       " 'conv1_2_2',\n",
       " 'conv1_1_2']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mv.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "np.save('vgg_imagenet_sampled.npy', mv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "nmv = np.load('vgg_imagenet_sampled.npy').item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['fc6_1',\n",
       " 'fc6',\n",
       " 'fc6_2',\n",
       " 'fc8',\n",
       " 'fc7_1',\n",
       " 'fc7_2',\n",
       " 'conv4_1_2',\n",
       " 'conv4_2_2',\n",
       " 'conv1_1',\n",
       " 'conv1_2',\n",
       " 'conv2_1_2',\n",
       " 'conv3_1_2',\n",
       " 'conv3_2_2',\n",
       " 'conv5_1',\n",
       " 'conv5_3',\n",
       " 'conv5_2',\n",
       " 'conv3_3_2',\n",
       " 'fc7',\n",
       " 'conv4_1',\n",
       " 'conv4_2',\n",
       " 'conv4_3',\n",
       " 'conv2_2_2',\n",
       " 'conv3_3',\n",
       " 'conv3_2',\n",
       " 'conv3_1',\n",
       " 'conv2_2',\n",
       " 'conv2_1',\n",
       " 'conv5_2_2',\n",
       " 'conv5_3_2',\n",
       " 'conv4_3_2',\n",
       " 'conv5_1_2',\n",
       " 'conv1_2_2',\n",
       " 'conv1_1_2']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nmv.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4096, 2048)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nmv['fc7_1']['weights'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4096, 4096)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nmv['fc7']['weights'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = np.array([10, 20, 30])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "b = np.repeat(a, 3, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rpn = np.load('../../rpn_data.npy').item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['rpn_conv/3x3',\n",
       " 'rpn_cls_score',\n",
       " 'conv5_1',\n",
       " 'conv5_3',\n",
       " 'conv5_2',\n",
       " 'conv4_1',\n",
       " 'conv4_2',\n",
       " 'conv4_3',\n",
       " 'conv3_3',\n",
       " 'conv3_2',\n",
       " 'conv3_1',\n",
       " 'conv1_1',\n",
       " 'conv1_2',\n",
       " 'conv2_2',\n",
       " 'rpn_bbox_pred',\n",
       " 'conv2_1']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rpn.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "new_vgg = np.load('vgg_imagenet_sampled.npy').item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['fc6_1',\n",
       " 'fc6',\n",
       " 'fc6_2',\n",
       " 'fc8',\n",
       " 'fc7_1',\n",
       " 'fc7_2',\n",
       " 'conv4_1_2',\n",
       " 'conv4_2_2',\n",
       " 'conv1_1',\n",
       " 'conv1_2',\n",
       " 'conv2_1_2',\n",
       " 'conv3_1_2',\n",
       " 'conv3_2_2',\n",
       " 'conv5_1',\n",
       " 'conv5_3',\n",
       " 'conv5_2',\n",
       " 'conv3_3_2',\n",
       " 'fc7',\n",
       " 'conv4_1',\n",
       " 'conv4_2',\n",
       " 'conv4_3',\n",
       " 'conv2_2_2',\n",
       " 'conv3_3',\n",
       " 'conv3_2',\n",
       " 'conv3_1',\n",
       " 'conv2_2',\n",
       " 'conv2_1',\n",
       " 'rpn_conv/3x3',\n",
       " 'conv5_2_2',\n",
       " 'conv5_3_2',\n",
       " 'conv4_3_2',\n",
       " 'conv5_1_2',\n",
       " 'conv1_2_2',\n",
       " 'rpn_bbox_pred',\n",
       " 'rpn_cls_score',\n",
       " 'conv1_1_2']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_vgg.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for key in rpn.keys():\n",
    "    new_vgg[key] = {'weights' : rpn[key]['weights'], 'biases':rpn[key]['biases']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.save('mv3d_imagenet_sampled.npy', new_vgg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
